package com.youpai.platform.wallet.service.service.transaction;

import com.youpai.platform.wallet.exception.ChangeCoinResultException;
import com.youpai.platform.wallet.service.entity.dto.ChangeCoinTransactionDTO;
import com.youpai.platform.wallet.service.entity.po.WalletPO;
import com.youpai.platform.wallet.service.mapper.WalletRecordMapper;
import com.youpai.platform.wallet.service.mapper.WalletMapper;
import com.youpai.platform.wallet.web.entity.dto.ChangeCoinReq;
import com.youpai.platform.wallet.web.enums.ChangeCoinResultEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @date: 2023/2/18
 * @author: Welles
 * @version: 1.0
 * @description:
 */
@Component
public class WalletTransaction {

    @Resource
    private WalletMapper walletMapper;
    @Resource
    private WalletRecordMapper walletRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    public void changeCoin(ChangeCoinTransactionDTO changeCoinTransactionDTO) {
        int changeCoinSuccess = walletMapper.changeCoin(changeCoinTransactionDTO.getWalletId(), changeCoinTransactionDTO.getWithdrawCoin(), changeCoinTransactionDTO.getOnlyEmployCoin());
        if (changeCoinSuccess < 1) {
            throw new ChangeCoinResultException(ChangeCoinResultEnum.COIN_NOT_ENOUGH);
        }
        int insertWalletRecordSuccess = walletRecordMapper.insertWalletRecord(changeCoinTransactionDTO.getWalletRecordPO());
        if (insertWalletRecordSuccess < 1) {
            throw new ChangeCoinResultException(ChangeCoinResultEnum.CHANGE_COIN_ERROR);
        }
    }
}
